/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework.http;

/**
 * This class represents a component defined by the HTTP protocol (RFC-2616).
 */
public class MessageHeaderElementParameter implements Processable {

	private String headerElementParameter;
	private String name;
	private String value;

	/**
	 * Creates a message header element parameter.
	 * The created message header element parameter needs to be parsed before the object can be used.
	 * 
	 * @param headerElementParameter The message header element parameter string containing a name and a value.
	 */
	public MessageHeaderElementParameter(String headerElementParameter) {
		this.headerElementParameter = headerElementParameter;
	}
	
	/**
	 * Creates a message header element parameter.
	 * 
	 * @param name The name.
	 * @param value The value.
	 */
	public MessageHeaderElementParameter(String name, String value) {
		this.name = name;
		this.value = value;
	}
	
	@Override
	public void parse() {
		String[] parameter = this.headerElementParameter.split("\\s*=\\s*");
		this.name = parameter[0];
		if(parameter.length == 2){
			this.value = parameter[1];
		}
	}
	
	@Override
	public String compose() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(this.name);
		if(this.value != null){
			if(!this.value.isEmpty()){
				stringBuilder.append("=");
				stringBuilder.append(this.value);
			}
		}
		return stringBuilder.toString();
	}
	
	@Override
	public String toString() {
		return this.compose();
	}
	
	/**
	 * Returns the name of the parameter.
	 * 
	 * @return The name.
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Returns the value of the parameter.
	 * 
	 * @return The parameter.
	 */
	public String getValue() {
		return this.value;
	}

}
